package com.fuze.navi.filter;

import com.fuze.navi.common.Constant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author sepc_
 * @date 5/10/2021 16:31
 */
@Slf4j
@Component
public class CrossFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        Map<String, String[]> map = request.getParameterMap();


        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String header = httpRequest.getHeader(Constant.AUTH_TOKEN);
        HttpServletResponse res = (HttpServletResponse) response;
        res.addHeader("Access-Control-Allow-Origin", "*");
        res.addHeader("Access-Control-Allow-Credentials", "true");
        res.addHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT,OPTIONS");
        res.addHeader("Access-Control-Allow-Headers", "Content-Type,X-CAF-Authorization-Token,sessionToken,X-TOKEN,AUTH_TOKEN");
        if (((HttpServletRequest) request).getMethod().equals(Constant.OPTIONS_METHOD)) {
            response.getWriter().println("ok");
            return;
        }
        chain.doFilter(request, response);
    }
}
